<template>
  <el-dialog
    :title="params.title || '选择参数'"
    width="30%"
    :visible.sync="params.visible"
  >
    <div>
      <div>
        <compTree
          ref="tree"
          :show-search="false"
          :tree-show-checkbox="params.ismulti"
          :tree-data="treeData"
          :comp-tree-height="400"
          :tree-node-select="treeNodeSelect"
          :tree-default-expanded-keys="DefaultExpandednode"
          @treeNodeCheck="treeNodeCheck"
        />
      </div>
    </div>
    <dialog-btn slot="footer" />
  </el-dialog>
</template>

<script>
/**
 * 入参
 *  title 当前弹窗显示的标题
 *  ismutil 是否多选
 */
import { getparamslist } from '@/api/flow'
import { ParamArr, SQLTable } from '@/../public/js/paramarr'
import Global from '@/../public/js/global'

export default {
  components: {},
  props: {
    params: {
      type: Object,
      default() {
        return {
          title: '选择变量',
          ismutil: false,
          textName: '' // 根据名称来回显
        }
      }
    }
  },
  data() {
    return {
      ptClass: 0,
      ptParams: 1,
      ptLocal: 2,
      treeData: [],
      textName: '',
      DefaultExpandednode: [],
      node: [] // 当前点击的节点
    }
  },
  created() {
    this.initWindow()
  },
  methods: {
    initWindow() {
      if (this.params.textName !== '' && this.params.textName.length > 10) {
        var name = this._.cloneDeep(this.params.textName).substring(8, this.params.textName.length - 2).split(',')
        if (name.length > 1) {
          this.textName = name[name.length - 1].substring(1)// 去除前导单引号
        } else {
          this.textName = name[0]
        }
      }
      getparamslist(new ParamArr()).then((vPar) => {
        this.node = []
        this.lpfInitWindow(vPar)
      })
    },
    lpfInitWindow(vPar) {
      var nodes = []
      var nodeMap = new Map()
      var table = new SQLTable(vPar.getTable('k_param'))
      table.first()
      while (!table.eof()) {
        var pnode = Global.getTreeviewPNode(
          nodeMap,
          nodes,
          table.getValueByName('PID')
        )
        var node = {}
        node['id'] = table.getValueByName('ID')
        // data
        node['texts'] = table.getValueByName('Name')
        if (table.getIntByName('pType') === 0) {
          node['pType'] = this.ptClass
          node['text'] = node.texts
          node['icon'] = 'wenjianyi'
        } else {
          node['pType'] = this.ptParams
          node['text'] = node.texts
          node['icon'] = 'fuzhi'
        }
        var pN = nodeMap.get(table.getValueByName('PID'))
        if (pN === undefined) {
          node['PName'] = ''
        } else {
          node['PName'] = pN['texts']
        }
        node['pid'] = table.getValueByName('PID')
        // add
        pnode.push(node)
        nodeMap.set(table.getValueByName('ID'), node)
        // next
        table.next()
      }
      var tablee = new SQLTable(vPar.getTable('k_param_detail'))
      tablee.first()
      while (!tablee.eof()) {
        var pnodee = Global.getTreeviewPNode(
          nodeMap,
          nodes,
          tablee.getValueByName('PID')
        )
        var nodee = {}
        // data
        var pNn = nodeMap.get(tablee.getValueByName('PID'))
        if (pNn) {
          nodee['texts'] = tablee.getValueByName('Name')
          nodee['text'] = tablee.getValueByName('Name')
          nodee['pid'] = tablee.getValueByName('PID')
          nodee['id'] = tablee.getValueByName('ID')
          nodee['pType'] = this.ptLocal
          nodee['PName'] = pNn['texts']
          nodee['icon'] = 'fuzhi'
          // add
          pnodee.push(nodee)
          nodeMap.set(tablee.getValueByName('ID'), nodee)
        } else {
          nodee['icon'] = 'fuzhi'
        }
        // next
        tablee.next()
      }
      this.treeData = nodes
      this.search(this.treeData)
    },
    search(data) {
      for (let i = 0; i < data.length; i++) {
        if (data[i].text === this.textName) {
          if (data[i].pid !== undefined && data[i].pid !== '') {
            this.DefaultExpandednode.push(data[i].pid)
          }
          this.$refs.tree.setCurrentKey(data[i].id)
          break
        }
        if (data[i].nodes !== undefined && data[i].nodes.length !== 0) {
          this.search(data[i].nodes)
        }
      }
    },
    treeNodeSelect(node) {
      this.node = node
    },
    // 节点点击事件 获取所有选中用户
    treeNodeCheck(a, b) {
      this.checkedNodes = b.checkedNodes // 选中的所有节点
    },
    // 过滤出选中的节点
    getReturn() {
      const checkedNodes = this.checkedNodes.filter((item) => {
        return item.type === 1
      })
      const arrID = []
      const arrName = []
      for (let i = 0; i < checkedNodes.length; i++) {
        if (checkedNodes[i].type === 1) {
          arrID.push(checkedNodes[i].id)
          arrName.push(checkedNodes[i].name)
        }
      }
      return { arrID, arrName }
    },
    confirm() {
      var nodee = this.node
      var sRst = ''
      if (nodee.length === 0) {
        this.$message({
          message: '请选择参数。',
          type: 'warning'
        })
        return
      } else {
        switch (nodee['pType']) {
          case this.ptClass:
            this.$message({
              message: '请选择参数。',
              type: 'warning'
            })
            return
          case this.ptParams:
            sRst = Global.strFormat(
              '获取全局参数({0})',
              Global.QuotedStr(nodee['texts'])
            )
            break
          case this.ptLocal:
            sRst = Global.strFormat(
              '获取机器参数({0},{1})',
              Global.QuotedStr(nodee['PName']),
              Global.QuotedStr(nodee['texts'])
            )
            break
        }
        if (this.params.confirm !== undefined) {
          this.params.confirm(sRst)
        } else {
          this.$emit('confirm', sRst)
        }
      }
    }
  }
}
</script>

<style scoped>
</style>
